/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProvaVisualizacaoGUI.java
 *
 * Created on 23/06/2011, 23:15:21
 */
package GUI;

import bo.DisciplinaBO;
import entidades.Questao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import logic.XMLer;

/**
 *
 * @author Luan
 */

public class ProvaVisualizacaoGUI extends javax.swing.JFrame {
    List<Questao> listaQuestoes = new ArrayList<Questao>();
    List<Questao> listaQuestoesAux = new ArrayList<Questao>();
    DefaultListModel listQuestoesSelecionadas = new DefaultListModel();
    DefaultListModel listQuestoesOrdenadas = new DefaultListModel();
    DefaultListModel listModelPontuacao = new DefaultListModel();

    /** Creates new form ProvaVisualizacaoGUI */
    public   ProvaVisualizacaoGUI (List<Questao> listaQuestoes) {
        if(!listaQuestoes.isEmpty()){
            DefaultListModel listModel = new DefaultListModel();
            initComponents();
            this.listaQuestoes.clear();
            this.listaQuestoes.addAll(listaQuestoes);
            for(Questao questao : this.listaQuestoes)
                listModel.addElement(questao.getEnunciadoQuestao());
            jListQuestoesSelec.setModel(listModel);
            listQuestoesSelecionadas = listModel;
        }
        else
            initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label4 = new java.awt.Label();
        label6 = new java.awt.Label();
        jButtonImprimirProva = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListQuestoesSelec = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListQuestoesOrd = new javax.swing.JList();
        jButtonInserir = new javax.swing.JButton();
        jButtonRetirar = new javax.swing.JButton();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        label3 = new java.awt.Label();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListPontuacao = new javax.swing.JList();
        label5 = new java.awt.Label();
        jButtonAlterarPontuacao = new javax.swing.JButton();

        label4.setFont(new java.awt.Font("Dialog", 1, 12));
        label4.setText("Questões ordenadas");

        label6.setFont(new java.awt.Font("Dialog", 1, 12));
        label6.setText("Questões ordenadas");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButtonImprimirProva.setText("Imprimir");
        jButtonImprimirProva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonImprimirProvaActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jListQuestoesSelec);

        jScrollPane2.setViewportView(jListQuestoesOrd);

        jButtonInserir.setText("Inserir >>");
        jButtonInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonInserirActionPerformed(evt);
            }
        });

        jButtonRetirar.setText("<< Retirar");
        jButtonRetirar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRetirarActionPerformed(evt);
            }
        });

        label1.setFont(new java.awt.Font("Dialog", 1, 12));
        label1.setText("Questões selecionadas");

        label2.setFont(new java.awt.Font("Dialog", 1, 12));
        label2.setText("Questões ordenadas");

        label3.setFont(new java.awt.Font("Dialog", 1, 12));
        label3.setText("Favor incluir as questões selecionadas na ordem em que estas devem aparecer na prova.");

        jScrollPane3.setViewportView(jListPontuacao);

        label5.setFont(new java.awt.Font("Dialog", 1, 12));
        label5.setText("Pontuação");

        jButtonAlterarPontuacao.setText("Alterar ");
        jButtonAlterarPontuacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAlterarPontuacaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(label3, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButtonInserir)
                            .addComponent(jButtonRetirar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButtonImprimirProva, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonAlterarPontuacao)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jButtonInserir)
                        .addGap(68, 68, 68)
                        .addComponent(jButtonRetirar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE))))
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonImprimirProva)
                    .addComponent(jButtonAlterarPontuacao))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonRetirarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRetirarActionPerformed
        int index = listQuestoesOrdenadas.indexOf(jListQuestoesOrd.getSelectedValue().toString());
        listQuestoesSelecionadas.addElement(jListQuestoesOrd.getSelectedValue().toString());
        listModelPontuacao.remove(index);
        jListPontuacao.setModel(listModelPontuacao);
        for(int i =0 ;i<listQuestoesSelecionadas.size();i++)
            jListQuestoesSelec.setModel(listQuestoesSelecionadas);

        listQuestoesOrdenadas.removeElement(jListQuestoesOrd.getSelectedValue().toString());
        jListQuestoesOrd.setModel(listQuestoesOrdenadas);
        
        AtualizarListaQuestoesProva(-1,null);
    }//GEN-LAST:event_jButtonRetirarActionPerformed

    private void jButtonInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonInserirActionPerformed
        Double pontuacaoQuestao;
        String pontoQuestao = null;
        pontoQuestao = JOptionPane.showInputDialog("Qual será a  pontuação desta questão na prova?");
        listModelPontuacao.addElement(pontoQuestao);
        jListPontuacao.setModel(listModelPontuacao);
        int index = listModelPontuacao.indexOf(pontoQuestao);
        pontuacaoQuestao = Double.parseDouble(pontoQuestao);

        listQuestoesOrdenadas.addElement( jListQuestoesSelec.getSelectedValue().toString());
         for(int i =0 ;i<listQuestoesOrdenadas.size();i++)
                  jListQuestoesOrd.setModel(listQuestoesOrdenadas);

        listQuestoesSelecionadas.removeElement(jListQuestoesSelec.getSelectedValue().toString());
        jListQuestoesSelec.setModel(listQuestoesSelecionadas);

        AtualizarListaQuestoesProva(index,pontuacaoQuestao);

    }//GEN-LAST:event_jButtonInserirActionPerformed

    private void jButtonImprimirProvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonImprimirProvaActionPerformed
        String nomeArquivo = null;
        nomeArquivo = JOptionPane.showInputDialog("Qual será o nome da prova?");
        try {
            XMLer.escreverDadosQuestoes(nomeArquivo, listaQuestoesAux, listModelPontuacao);
        } catch (IOException ex) {
            Logger.getLogger(ProvaVisualizacaoGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        XMLer.ProcessaGabarito(nomeArquivo, listaQuestoes , listModelPontuacao);
    }//GEN-LAST:event_jButtonImprimirProvaActionPerformed

    private void jButtonAlterarPontuacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAlterarPontuacaoActionPerformed
        int index=-1;

        if ((jListPontuacao.getSelectedIndex())>=0)
            index = jListPontuacao.getSelectedIndex();
        else if (jListQuestoesOrd.getSelectedIndex()>=0)
            index = jListQuestoesOrd.getSelectedIndex();
        else
            JOptionPane.showMessageDialog(null,"Selecione a questão correspondente ou a pontuação a ser alterada");

        if (index !=-1){
            Double pontuacaoQuestao;
            String pontoQuestao = null;
            pontoQuestao = JOptionPane.showInputDialog("Qual será a pontução desta questão na prova?");
            listModelPontuacao.remove(index);
            listModelPontuacao.add(index, pontoQuestao);
            jListPontuacao.setModel(listModelPontuacao);
            pontuacaoQuestao = Double.parseDouble(pontoQuestao);
            AtualizarListaQuestoesProva(index,pontuacaoQuestao);
        }
    }//GEN-LAST:event_jButtonAlterarPontuacaoActionPerformed

    private void AtualizarListaQuestoesProva (int index, Double pontuacaoQuestao){
        listaQuestoesAux.clear();
        for(int i = 0 ;i < listaQuestoes.size(); i++){
            for(int j = 0; j < listQuestoesOrdenadas.size(); j++){
                if((listaQuestoes.get(i).getEnunciadoQuestao().equalsIgnoreCase(listQuestoesOrdenadas.get(j).toString()))){
                   if(j == index)
                       listaQuestoes.get(i).setValorPontuacao(pontuacaoQuestao);
                   listaQuestoesAux.add(listaQuestoes.get(i));
                   break;     
                }
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ProvaVisualizacaoGUI(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAlterarPontuacao;
    private javax.swing.JButton jButtonImprimirProva;
    private javax.swing.JButton jButtonInserir;
    private javax.swing.JButton jButtonRetirar;
    private javax.swing.JList jListPontuacao;
    private javax.swing.JList jListQuestoesOrd;
    private javax.swing.JList jListQuestoesSelec;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.Label label5;
    private java.awt.Label label6;
    // End of variables declaration//GEN-END:variables
}
